package com.dreamsail.core.frame;

import com.dreamsail.core.constant.ErrorMessage;
import com.dreamsail.core.model.system.BaseException;
import com.dreamsail.core.model.system.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.sql.SQLException;

/**
 * 异常处理
 */
@ControllerAdvice
@Slf4j
public class ErrorHandler {

    // 系统异常处理
    @ExceptionHandler(BaseException.class)
    public Result baseExceptionHandler(BaseException e){
        e.printStackTrace();
        return new Result(e.getCode(),e.getMsg(),null);
    }

    // sql 异常处理
    @ExceptionHandler(SQLException.class)
    public Result sqlExceptionHandler(SQLException e){
        e.printStackTrace();
        return new Result(ErrorMessage.SQL_ERROR.getCode(),ErrorMessage.SQL_ERROR.getMsg(),null);
    }

    // 所有未捕获异常处理
    @ExceptionHandler(Exception.class)
    public Result allExceptionHandler(Exception e){
        e.printStackTrace();
        return new Result(ErrorMessage.SYS_ERROR.getCode(),ErrorMessage.SYS_ERROR.getMsg(),null);
    }
}
